Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug Fix] Grouping Cluster Resources by group and Kind #875

Closed

Conversation

arunikadhal
Copy link

What are you trying to accomplish with this PR?
Fixes #873

How is this accomplished?
Instead of grouping by kind in DeployTask#discover_resources, we now group by group and kind to avoid picking the wrong CRD when there are duplicates of the same kind.

What could go wrong?
...

@arunikadhal arunikadhal requested a review from a team as a code owner February 25, 2022 15:34
@arunikadhal arunikadhal requested review from d1egoaz and peiranliushop and removed request for a team February 25, 2022 15:34
@@ -238,9 +238,10 @@ def secrets_from_ejson
def discover_resources
@logger.info("Discovering resources:")
resources = []
crds_by_kind = cluster_resource_discoverer.crds.group_by(&:kind)
crds_by_group_kind = cluster_resource_discoverer.crds.group_by(&:group_kind)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there tests to cover this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be great to test this change, this is supposed to change the order of the items

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added new tests to cover these changes

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is supposed to change the order of the items

I don't quite understand what you mean by this, could you explain please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, sorry, I misread the issue in #873 and I thought a consequence of this PR is that it'll change the order of the items in some list, however, this change is about grouping differently. Sorry for the noise

@d1egoaz
Copy link
Contributor

d1egoaz commented Feb 25, 2022

it seems some tests are broken:

Error:
KraneDeployTest#test_raise_on_yaml_missing_kind:
TypeError: no implicit conversion of nil into String

@template_sets.with_resource_definitions(current_sha: @current_sha, bindings: @bindings) do |r_def|
crd = crds_by_kind[r_def["kind"]]&.first
grouping, version = r_def.dig("apiVersion").split("/")
group = version ? grouping : "core"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does mean "core" in this context?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When extracting group, lib/krane/kubernetes_resource.rb returns the group defined in the resource definition or core. We need to do something similar here for r_def but we cannot use the same method in lib/krane/kubernetes_resource.rb so I decided to replicate the behaviour here instead (hence the core).

lib/krane/deploy_task.rb Outdated Show resolved Hide resolved
@@ -238,9 +238,10 @@ def secrets_from_ejson
def discover_resources
@logger.info("Discovering resources:")
resources = []
crds_by_kind = cluster_resource_discoverer.crds.group_by(&:kind)
crds_by_group_kind = cluster_resource_discoverer.crds.group_by(&:group_kind)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is supposed to change the order of the items

I don't quite understand what you mean by this, could you explain please?

@@ -59,7 +59,7 @@ def status
end

def type
kind
group_kind
Copy link
Contributor

@timothysmith0609 timothysmith0609 Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For other reviewers:

#type is already a somewhat overloaded method. In particular, KubernetesResource#type has as body:

    def type
      @type || self.class.kind
    end

#type is mainly used internally, most critically in DeployTask#deploy_has_priority_resources?, though it's value also appears in the tool's output.

#type is also aliased to KubernetesResource#kubectl_resource_type, which is used by ResourceCache. There is already a case where #type is overridden to include the group suffix.

All this to say, I'm hoping the use of group_kind here isn't too controversial :)

Comment on lines +55 to +58
def group_kind
"#{kind}.#{group}"
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behaviour of CustomResourceDefinition#kind and #group is a little strange (you might expect it to return CRD and apiextensions, respectively), but since this is previous behaviour I think it's ok to leverage it

definition: { "kind" => "UnitTest", "metadata" => { "name" => "test" } })
definition: {
"kind" => "UnitTest",
"apiVersion" => "stable.example.io/v1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to add apiVersion to make these tests pass (since we now parse group info). Since all k8s objects require this field, it makes sense to add it instead of accommodating its absence

@@ -450,7 +487,7 @@ def test_deploying_crs_with_invalid_crd_conditions_fails
end
end
assert_deploy_failure(result)
prefixed_name = add_unique_prefix_for_test("Customized-with-customized-params")
prefixed_name = add_unique_prefix_for_test('Customized.stable.example.io-with-customized-params').to_s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most visible consequence of our change, and I would argue it's a feature: CRDs are now logged showing both their group and kind. This should make things easier to understand in the case of multiple kinds among difference groups

@timothysmith0609
Copy link
Contributor

timothysmith0609 commented Mar 10, 2022

You'll also need to fix the CR logic in global_deploy_task, as well (and add test)

@arunikadhal
Copy link
Author

arunikadhal commented Mar 11, 2022

You'll also need to fix the CR logic in global_deploy_task, as well (and add test)

There seems to be a different issue in global_deploy_task, I have opened a new issue to fix the kind & name discrepancy here.

@timothysmith0609
Copy link
Contributor

Closing as stale

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong CRD may be referenced by Custom Resource with duplicate Kind
4 participants